<%@ page language="java" contentType="text/html; charset=utf8"
         pageEncoding="utf8" %>
<% String path = request.getContextPath(); %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0"/>
    <meta name="apple-mobile-web-app-capable" content="yes"/>
    <meta name="format-detection" content="telephone=no"/>
    <link rel="stylesheet" href="<%=path%>/assets/css/admin/authority.css">
    <link rel="stylesheet" href="<%=path%>/assets/css/main.css">
    <link rel="stylesheet" href="<%=path%>/assets/css/oldie.css">
    <link rel="stylesheet" href="<%=path%>/assets/css/jquery-ztree/zTreeStyle.css">
    <script src="${pageContext.request.contextPath}/assets/js-lib/jquery/jquery.min.js"></script>
    <script src="${pageContext.request.contextPath}/assets/js-lib/jquery/jquery.ztree.core-3.4.js"></script>

    <title>${title}</title>
    <style>
        html, body {
            height: 100%;
        }
    </style>


</head>
<body>
<!-- 引入头页 -->
<jsp:include page="../../common/head.jsp"></jsp:include>
<div class="container" style="height:auto;min-height:88%;margin-top: 70px;margin-bottom:10px;">
    <div class="row">
        <!-- 左菜单begin -->

        <div class="col-sm-2" id="menu_left">
            <div class="list-firstgroup list-layout">所有角色</div>

            <div class="list-group" id="leftConterx">
                 <c:forEach items="${roleList}" varStatus="status" var="var">
                     <a class="list-group-item <c:if test="${status.index eq '0'}">active</c:if>" id="${var.id}" href="javascript:;">${var.roleName }</a>
                 </c:forEach>
            </div>
        </div>
        <!-- 左菜单end -->


        <div class="col-md-10">
            <div class="row">
                <div class="col-md-6">
            <ol class="breadcrumb">
                <li>当前位置：</li>
                <li class="active">权限管理</li>
            </ol>
                </div>
            </div>

            <div class="row">
                <form id="saveForm" method="post">
                <div class="col-md-">
                    <div class="authority_left" id="authority_left">
                        <div class="category-filters" style="border-radius:0">
                            <button type="button" class="btn btn-primary btn-sm userbtn" id="saveBt">保存</button>
                            <div class="clear"></div>
                        </div>
                        <ul id="menuTree" class="ztree" ></ul>
                        <input type="hidden" id="checkTreeNode" value=""/>
                        <input type="hidden" id="authorityId" value="${checkedNodes}"/>
                    </div>
                </div>
                </form>

                <div class="col-md-8">


                    <div class="category-filters">
                        <!-- 搜索栏 begin -->
                        <form id="search-form" class="form-inline userNNsearch"
                              action="" method="post">
                            <div class="form-group">
                                <input type="text" id="uNameOrNum" name="uNameOrNum"
                                       value="" class="form-control" placeholder="用户编号或者用户名">
                            </div>
                            <a class="btn btn-primary" id="searchBtn" onclick="creatPageList(1)">搜索</a>
                        </form>
                        <div class="clear"></div>
                    </div><input type="hidden" id="thisRoleId" value="${roleId}"/>

                    <!--数据begin-->
                    <div id="contextDiv">

                    </div>
                    <!--数据end-->
                </div>

            </div>





        </div>


    </div>


</div>




<!--尾部-->
<jsp:include page="../../common/footer.jsp"></jsp:include>


<!--查询请求参数-->
<script type="text/javascript">
    var Query = "${pageContext.request.contextPath}/sys/auth/contextList.do";
</script>
<!--js-->
<script src="${pageContext.request.contextPath}/js/sys/authority/list.js"></script>

<script type="text/javascript">

    var zTreeObj;
    var setting = {
        check: {
            enable: true,
            chkboxType: {"Y":"ps", "N":"ps"}
        },
        view: {
            selectedMulti: false,
        },
        data: {
            simpleData: {
                enable: true
            }
        },
        callback:{
            onCheck: zTreeOnCheck
        }
    };

    var ztreeNode = [];
    function zTreeOnCheck(event, treeId, treeNode) {
        ztreeNode.length = 0;
        var treeObj = $.fn.zTree.getZTreeObj("menuTree");
        var nodes = treeObj.getCheckedNodes(true);
        for(var i = 0; i < nodes.length; i++){
         //   if (!nodes[i].isParent) {
                ztreeNode.push(nodes[i].trueId);
         //   }
        }
            var authorityIds = ztreeNode.join(",");
            $("#checkTreeNode").val(authorityIds);

    };

    function getMenuTree() {
        $.post('getMenuTree.do',{},function (data) {
           // alert(data)
            zTreeObj = $.fn.zTree.init($("#menuTree"), setting, data);
            getTree();
        },'json');
    }


    function getTree() {
        $.ajax({
            type: "POST",
            url: "getTree.do",
            data: {roleId:$('.active').attr('id')},
            cache: false,
            async: true,

            success: function (data2) {
                ztreeNode.length = 0;
                var data1 = eval('('+data2+')');
                var dataList = data1['checkedNodes'];
                    var authorityId = $("#authorityId").val(dataList);
                    if (authorityId != null && authorityId != '') {
                        var zTreeObj = $.fn.zTree.getZTreeObj("menuTree");
                        var zTree = zTreeObj.getCheckedNodes(false);
                        var nodeId = dataList;
                        if(nodeId != null){
                            var arr = nodeId.split(",");
                            for (var i = 0; i < zTree.length; i++) {
                                for (var j = 0; j < arr.length; j++) {
                                    if (zTree[i].trueId == arr[j]) {
                                        ztreeNode.push(arr[j]);
                                        zTreeObj.checkNode(zTree[i], true);
                                    }
                                }
                            }
                            var authorityIds = ztreeNode.join(",");
                            $("#checkTreeNode").val(authorityIds);
                        }else{
                            $("#checkTreeNode").val("");
                        }

                    }
            }

        });


    }

    $("#saveBt").click(function () {

        var treeObj = $.fn.zTree.getZTreeObj("menuTree");
        var nodes = treeObj.getCheckedNodes(true);
        if(nodes.length == 0){
            errorRemind($("body"), "至少分配一个权限");
            return false;
        }else{
            $.post('save.do',{authorityIds:$("#checkTreeNode").val(),roleId:$('.active').attr('id')},function (data) {
                if (data.flag == "1") {
                    layer.msg("保存成功", {icon:6,time:1000,shade: 0.01});
                } else {
                    errorRemind($("body"), "保存失败");
                }
            },'json');
        }


    });

    $(".list-group-item").click(function () {
        $('.list-group-item').removeClass('active');
        $(this).addClass('active');
        var roleId = $(this).attr('id');
        getMenuTree();
        $("#uNameOrNum").val("");
        creatPageList(1);
    });


    $(function () {
        getMenuTree();
    })


</script>
</body>

</html>